package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.outside.admission.query.SupplierOutsideOrderAdmissionTicketDTO;
import com.ctshk.rpc.booking.dto.outside.admission.query.SupplierOutsideOrderAdmissionTicketTouristDTO;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderAdmissionTicket;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderAdmissionTicketTourist;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderAdmissionTicketMapper;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderAdmissionTicketTouristMapper;
import com.ctshk.rpc.booking.req.outside.admission.save.SupplierOutsideOrderAdmissionTicketReq;
import com.ctshk.rpc.booking.service.ISupplierOutsideOrderAdmissionTicketService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 站外门票预订信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-25
 */
@DubboService
public class SupplierOutsideOrderAdmissionTicketServiceImpl implements ISupplierOutsideOrderAdmissionTicketService {

    @Autowired
    SupplierOutsideOrderAdmissionTicketMapper supplierOutsideOrderAdmissionTicketMapper;
    @Autowired
    SupplierOutsideOrderAdmissionTicketTouristMapper supplierOutsideOrderAdmissionTicketTouristMapper;

    @Override
    public void save(Long supplierOrderId, Long userId, LocalDateTime nowDateTime, List<SupplierOutsideOrderAdmissionTicketReq> supplierOutsideOrderAdmissionTicketReqList) {
        if (supplierOrderId != null) {
            //删除
            QueryWrapper<SupplierOutsideOrderAdmissionTicket> outsideOrderAdmissionTicketWrapper = new QueryWrapper<>();
            outsideOrderAdmissionTicketWrapper.lambda().eq(SupplierOutsideOrderAdmissionTicket::getOrderId, supplierOrderId);
            supplierOutsideOrderAdmissionTicketMapper.delete(outsideOrderAdmissionTicketWrapper);
            QueryWrapper<SupplierOutsideOrderAdmissionTicketTourist> outsideOrderAdmissionTicketTouristWrapper = new QueryWrapper<>();
            outsideOrderAdmissionTicketTouristWrapper.lambda().eq(SupplierOutsideOrderAdmissionTicketTourist::getOrderId, supplierOrderId);
            supplierOutsideOrderAdmissionTicketTouristMapper.delete(outsideOrderAdmissionTicketTouristWrapper);
        }
        //新增门票信息
        List<SupplierOutsideOrderAdmissionTicket> supplierOutsideOrderAdmissionTickets = new ArrayList<>();
        List<SupplierOutsideOrderAdmissionTicketTourist> supplierOutsideOrderAdmissionTicketTourists = new ArrayList<>();
        supplierOutsideOrderAdmissionTicketReqList.forEach(outsideOrderAdmissionTicketReq -> {
            SupplierOutsideOrderAdmissionTicket supplierOutsideOrderAdmissionTicket = EntityUtil.copy(outsideOrderAdmissionTicketReq, SupplierOutsideOrderAdmissionTicket.class);
            supplierOutsideOrderAdmissionTicket.setId(SnowflakeIdWorker.nextId());
            supplierOutsideOrderAdmissionTicket.setOrderId(supplierOrderId);
            supplierOutsideOrderAdmissionTicket.setCreateId(userId);
            supplierOutsideOrderAdmissionTicket.setGmtCreate(nowDateTime);
            supplierOutsideOrderAdmissionTicket.setModifiedId(userId);
            supplierOutsideOrderAdmissionTicket.setGmtModified(nowDateTime);
            supplierOutsideOrderAdmissionTicket.setIsDeleted(IsDeletedCode.NO.getCode());
            supplierOutsideOrderAdmissionTickets.add(supplierOutsideOrderAdmissionTicket);

            if (CollectionUtils.isNotEmpty(outsideOrderAdmissionTicketReq.getSupplierOutsideOrderAdmissionTicketTouristReqList())) {
                //新增旅客类型信息
                outsideOrderAdmissionTicketReq.getSupplierOutsideOrderAdmissionTicketTouristReqList().forEach(outsideOrderAdmissionTicketTouristReq -> {
                    SupplierOutsideOrderAdmissionTicketTourist supplierOutsideOrderAdmissionTicketTourist = EntityUtil.copy(outsideOrderAdmissionTicketTouristReq, SupplierOutsideOrderAdmissionTicketTourist.class);
                    supplierOutsideOrderAdmissionTicketTourist.setId(SnowflakeIdWorker.nextId());
                    supplierOutsideOrderAdmissionTicketTourist.setOrderId(supplierOrderId);
                    supplierOutsideOrderAdmissionTicketTourist.setAdmissionTicketId(supplierOutsideOrderAdmissionTicket.getId());
                    supplierOutsideOrderAdmissionTicketTourist.setCreateId(userId);
                    supplierOutsideOrderAdmissionTicketTourist.setGmtCreate(nowDateTime);
                    supplierOutsideOrderAdmissionTicketTourist.setModifiedId(userId);
                    supplierOutsideOrderAdmissionTicketTourist.setGmtModified(nowDateTime);
                    supplierOutsideOrderAdmissionTicketTourist.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierOutsideOrderAdmissionTicketTourists.add(supplierOutsideOrderAdmissionTicketTourist);

                });
            }
        });
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderAdmissionTickets)) {
            supplierOutsideOrderAdmissionTicketMapper.insertBatchSomeColumn(supplierOutsideOrderAdmissionTickets);
        }
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderAdmissionTickets)) {
            supplierOutsideOrderAdmissionTicketTouristMapper.insertBatchSomeColumn(supplierOutsideOrderAdmissionTicketTourists);
        }
    }

    @Override
    public List<SupplierOutsideOrderAdmissionTicketDTO> selectByOrderId(Long id) {
        List<SupplierOutsideOrderAdmissionTicketDTO> supplierOutsideOrderAdmissionTicketDTOS = new ArrayList<>();
        //查询机票列表
        QueryWrapper<SupplierOutsideOrderAdmissionTicket> AdmissionTicketWrapper = new QueryWrapper();
        AdmissionTicketWrapper.lambda().eq(SupplierOutsideOrderAdmissionTicket::getOrderId, id);
        List<SupplierOutsideOrderAdmissionTicket> supplierOutsideOrderAdmissionTickets = supplierOutsideOrderAdmissionTicketMapper.selectList(AdmissionTicketWrapper);
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderAdmissionTickets)) {
            supplierOutsideOrderAdmissionTicketDTOS = EntityUtil.copyList(supplierOutsideOrderAdmissionTickets, SupplierOutsideOrderAdmissionTicketDTO.class);
            supplierOutsideOrderAdmissionTicketDTOS.forEach(AdmissionTicket -> {
                //查询机票旅客类型列表
                QueryWrapper<SupplierOutsideOrderAdmissionTicketTourist> AdmissionTicketTouristWrapper = new QueryWrapper();
                AdmissionTicketTouristWrapper.lambda().eq(SupplierOutsideOrderAdmissionTicketTourist::getOrderId, id);
                AdmissionTicketTouristWrapper.lambda().eq(SupplierOutsideOrderAdmissionTicketTourist::getAdmissionTicketId, AdmissionTicket.getId());
                List<SupplierOutsideOrderAdmissionTicketTourist> supplierOutsideOrderAdmissionTicketTourists = supplierOutsideOrderAdmissionTicketTouristMapper.selectList(AdmissionTicketTouristWrapper);
                if (CollectionUtils.isNotEmpty(supplierOutsideOrderAdmissionTicketTourists)) {
                    List<SupplierOutsideOrderAdmissionTicketTouristDTO> supplierOutsideOrderAdmissionTicketTouristDTOList = EntityUtil.copyList(supplierOutsideOrderAdmissionTicketTourists, SupplierOutsideOrderAdmissionTicketTouristDTO.class);
                    AdmissionTicket.setSupplierOutsideOrderAdmissionTicketTouristDTOList(supplierOutsideOrderAdmissionTicketTouristDTOList);
                }
            });
        }
        return supplierOutsideOrderAdmissionTicketDTOS;
    }
}
